package com.exam.controller;

import com.exam.entity.*;
import com.exam.serviceimpl.FillQuestionServiceImpl;
import com.exam.serviceimpl.JudgeQuestionServiceImpl;
import com.exam.serviceimpl.MultiQuestionServiceImpl;
import com.exam.serviceimpl.PaperServiceImpl;
import com.exam.util.ApiResultHandler;
import com.exam.util.FillQuestionPoiUtils;
import com.exam.util.JudgeQuestionPoiUtils;
import com.exam.util.MultiQuestionPoiUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@RestController
public class MultiQuestionController {
    @Autowired
    private JudgeQuestionServiceImpl judgeQuestionService;
    @Autowired
    private FillQuestionServiceImpl fillQuestionService;
    @Autowired
    private MultiQuestionServiceImpl multiQuestionService;
    @Autowired
    private PaperServiceImpl PaperServiceService;
    @GetMapping("/multiQuestionId")
    public ApiResult findOnlyQuestion() {
        MultiQuestion res = multiQuestionService.findOnlyQuestionId();
        return ApiResultHandler.buildApiResult(200,"查询成功",res);
    }

    @PostMapping("/MultiQuestion")
    public ApiResult add(@RequestBody MultiQuestion multiQuestion) {
        int res = multiQuestionService.add(multiQuestion);
        if (res != 0) {
            return ApiResultHandler.buildApiResult(200,"添加成功",res);
        }
        return ApiResultHandler.buildApiResult(400,"添加失败",res);
    }

    /**
     *导入
     */
    @PostMapping("/upload")
    public ApiResult importData(MultipartFile file, HttpServletRequest req) throws IOException {
        String examlx1 = file.getOriginalFilename();
        int res = 0;
        System.out.printf("examlxxlxlx=" + examlx1);
        List<examlx> examlx = null;
        if (examlx1.contains("填空题")) {
            examlx1 = "填空题";
            //        通过查数据库确定有几列并且确定每一列对应的字段。
//        PaperServiceImpl p1=new PaperServiceImpl();
            examlx = PaperServiceService.findAlllx(examlx1);
            System.out.printf("examlx===" + examlx);
            List<FillQuestion> topics = FillQuestionPoiUtils.parseFile2List(file, examlx);
            System.out.print("topics===" + topics);
//            执行插入。按行插入
            for (int i = 0; i < topics.size(); i++) {
                FillQuestion fillQuestion = new FillQuestion();
                fillQuestion.setQuestion(topics.get(i).getQuestion());
                fillQuestion.setAnalysis(topics.get(i).getAnalysis());
                fillQuestion.setAnswer(topics.get(i).getAnswer());
                fillQuestion.setLevel(topics.get(i).getLevel());
                fillQuestion.setScore(topics.get(i).getScore());
                fillQuestion.setSection(topics.get(i).getSection());
                fillQuestion.setSubject(topics.get(i).getSubject());
                res = fillQuestionService.add(fillQuestion);

            }
        }else if (examlx1.contains("选择题")) {
            examlx1 = "选择题";
            //        通过查数据库确定有几列并且确定每一列对应的字段。
//        PaperServiceImpl p1=new PaperServiceImpl();
            examlx = PaperServiceService.findAlllx(examlx1);
            System.out.printf("examlx===" + examlx);
            List<MultiQuestion> topics = MultiQuestionPoiUtils.parseFile2List(file, examlx);
            System.out.print("topics===" + topics);
//            执行插入。按行插入
            for (int i = 0; i < topics.size(); i++) {
                MultiQuestion multiQuestion = new MultiQuestion();
                multiQuestion.setQuestion(topics.get(i).getQuestion());
                multiQuestion.setAnalysis(topics.get(i).getAnalysis());
                multiQuestion.setRightAnswer(topics.get(i).getRightAnswer());
                multiQuestion.setLevel(topics.get(i).getLevel());
                multiQuestion.setScore(topics.get(i).getScore());
                multiQuestion.setSection(topics.get(i).getSection());
                multiQuestion.setSubject(topics.get(i).getSubject());
                multiQuestion.setAnswerA(topics.get(i).getAnswerA());
                multiQuestion.setAnswerB(topics.get(i).getAnswerB());
                multiQuestion.setAnswerC(topics.get(i).getAnswerC());
                multiQuestion.setAnswerD(topics.get(i).getAnswerD());
                res = multiQuestionService.add(multiQuestion);

            }
        }else if (examlx1.contains("判断题")) {
            examlx1 = "判断题";
            //        通过查数据库确定有几列并且确定每一列对应的字段。
//        PaperServiceImpl p1=new PaperServiceImpl();
            examlx = PaperServiceService.findAlllx(examlx1);
            System.out.printf("examlx===" + examlx);
            List<JudgeQuestion> topics = JudgeQuestionPoiUtils.parseFile2List(file, examlx);
            System.out.print("topics===" + topics);
//            执行插入。按行插入
            for (int i = 0; i < topics.size(); i++) {
                JudgeQuestion judgeQuestion = new JudgeQuestion();
                judgeQuestion.setQuestion(topics.get(i).getQuestion());
                judgeQuestion.setAnalysis(topics.get(i).getAnalysis());
                judgeQuestion.setAnswer(topics.get(i).getAnswer());
                judgeQuestion.setLevel(topics.get(i).getLevel());
                judgeQuestion.setScore(topics.get(i).getScore());
                judgeQuestion.setSection(topics.get(i).getSection());
                judgeQuestion.setSubject(topics.get(i).getSubject());
                res = judgeQuestionService.add(judgeQuestion);

            }
        }


        if (res != 0) {
            return ApiResultHandler.buildApiResult(200, "添加成功", res);
        }
        return ApiResultHandler.buildApiResult(400, "添加失败", res);
    }
}
